using UnityEngine;
using System.Collections;

public class MousePointer : MonoBehaviour {
	
	public GameObject background;
	
	public bool onlyOnClick = false;
	public bool showCursor = true;
	public bool keepZ = false;
	
	// Use this for initialization
	void Start () {
		if(Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer)
			onlyOnClick = true;
		if(!showCursor)
			Screen.showCursor = false;
	}
	
	// Update is called once per frame
	void Update () {
		followMouse();
		mouseOver();
	}
	
	private void followMouse(){
		if(!Input.GetMouseButton (0) && onlyOnClick )
			return;
		Camera mainCamera = Camera.main;
		Ray ray = mainCamera.ScreenPointToRay (Input.mousePosition);
		RaycastHit hit = new RaycastHit();
		if (!background.collider.Raycast(ray,  out hit, Mathf.Infinity))
			return;
		float z_backup = this.transform.position.z ; 
		this.transform.position = hit.point;
		if(keepZ){
			Vector3 pos = this.transform.position;
			pos.z = z_backup;
			this.transform.position = pos;
		}
	}
	
	void OnDestroy(){
		if(!showCursor)
			Screen.showCursor = true;
	}
	
	private void mouseOver(){
		if(showCursor)
			return;

		if (Mouse.isOnButton() ){
			//Screen.showCursor = true;
		}else Screen.showCursor = false;
	}
}
